import {
    removeNews,
    clickNews,
    recommendNews
} from '../../requestFunctions/newsRequestFunctions';
const app = getApp();

Component({
    externalClasses: [],
    properties: {
        recommendations: {
            type: Array,
            
        },
        // activeLabelId: {
        //     type: Number
        // }
    },
    data: {  
        labels: null,
        newsLists: null,
        labelTags: null,
        activeLabelIndex: null,
        activeLabelId: null,
        activeLabel: null,
        newsList: null,
        shownNews: null,
        showSelectLabelsModal: false
    },
    methods: {
        onTagTapped: function (e) {
            // console.log(e);
            const labelIndex = e.detail.tagIndex;
            const label = this.data.recommendations.map(rec => rec.label)[labelIndex];
            const labelId = label.id;
            const newsList = this.data.recommendations.map(rec => rec.newsList)[labelIndex];
            this.setData({
                activeLabelIndex: labelIndex,
                activeLabelId: labelId,
                activeLabel: label,
                newsList: newsList,
            });
        },
        onNewsRemoved: function (e) {
            const newsIndex = e.target.dataset.newsIndex;
            const oldNews = e.detail.news;
            const that = this;
            // console.log(newsIndex, oldNews);
            const recommendations = this.data.recommendations;
            const newsLists = this.data.newsLists;
            const newsList = this.data.newsList;
            removeNews(app.globalData.faiUserInfo.faiSessionKey, oldNews.id, newsList.map(news=>news.id), this.data.activeLabelId).then(newNews => {
                recommendations[that.data.activeLabelIndex].newsList[newsIndex] = newNews;
                newsLists[that.data.activeLabelIndex][newsIndex] = newNews;
                newsList[newsIndex] = newNews;
                that.setData({
                    recommendations: recommendations,
                    newsLists: newsLists,
                    newsList,
                    newsList
                });
            });
            // wx.request({
            //     url: 'https://wechat-app.guhaifengyun.com/news.json',
            //     success: (resp) => {
            //         const newNews = resp.data;
            //     }
            // });
        },
        onNewsTapped: function (e) {
            // console.log(e.detail);
            const newsIndex = e.target.dataset.newsIndex;
            const news = e.detail;
            this.setData({
                shownNews: news
            });
            clickNews(app.globalData.faiUserInfo.faiSessionKey, news.id).then((eventId) => {})
        },
        onNewsModalCloseButtonTapped: function (e) {
            this.setData({
                shownNews: null
            });
        },
        onSelectLabelsModalClose: function (e) {
            this.setData({
                showSelectLabelsModal: false
            });
        },
    },
    created: function (options) {
        // console.log('pseudoIndexPage', 'created', this);
        // 组件生命周期函数，在组件实例进入页面节点树时执行，注意此时不能调用 setData
    },
    attached: function () {
        // console.log('pseudoIndexPage', 'attached', this);
        // 组件生命周期函数，在组件实例进入页面节点树时执行
    },
    ready: function () {
        const that = this;

        // app.initUserInfo().then(() => {
        const newUser = app.globalData.faiUserInfo.newUser;
        if (newUser === true) {
            that.setData({
                showSelectLabelsModal: true
            });
        }

        recommendNews(app.globalData.faiUserInfo.faiSessionKey).then((recommendations) => {
            recommendations.sort((e1, e2) => {
                return e1.label.score - e2.label.score;
            });
            const labels = recommendations.map(rec => rec.label);
            const newsLists = recommendations.map(rec => rec.newsList);
            const activeLabelIndex = labels.length > 0 ? 0 : null;
            const activeLabelId = activeLabelIndex !== null ? labels[0].id : null;
            const activeLabel = activeLabelIndex !== null ? labels[0] : null;
            const newsList = activeLabelIndex !== null ? newsLists[0] : null;
            that.setData({
                labelTags: recommendations.map(rec => ({
                    id: rec.label.id,
                    title: rec.label.name,
                })),
                recommendations: recommendations,
                labels: labels,
                newsLists: newsLists,
                activeLabelIndex: activeLabelIndex,
                activeLabelId: activeLabelId,
                activeLabel: activeLabel,
                newsList: newsList,
            });
            // that.setData({
            //     recommendations: recs
            // });
        })
        // });

        // console.log('pseudoIndexPage', 'ready', this);
        // 组件生命周期函数，在组件布局完成后执行，此时可以获取节点信息（使用 SelectorQuery）
    },
    moved: function () {
        // console.log('pseudoIndexPage', 'moved', this);
        // 组件生命周期函数，在组件实例被移动到节点树另一个位置时执行
    },
    detached: function () {
        // console.log('pseudoIndexPage', 'detached', this);
        // 组件生命周期函数，在组件实例被从页面节点树移除时执行
    },
})